package com.java.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java.business.dao.ExpenditureMapper;
import com.java.business.model.Expenditure;
import com.java.business.model.PurchaseBill;
import com.java.business.service.ExpenditureService;
import com.java.common.model.Result;
import com.java.common.util.JwtUtil;
import com.java.manage.model.Company;
import com.java.manage.service.api.CompanyApiService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ExpenditureImpl implements ExpenditureService {

    @Autowired
    private ExpenditureMapper expenditureMapper;
    @DubboReference
    private CompanyApiService companyApiService;
    @Autowired
    private HttpServletRequest request;
    /**
     * 支出
     * @param expenditure
     * @return
     */
    @Transactional
    @Override
    public Result save(Expenditure expenditure) {
        expenditure.setType("2");
        expenditure.setCreateTime(new Date());
        expenditureMapper.save(expenditure);

        return Result.success();
    }

    @Transactional
    @Override
    public Result modify(Expenditure expenditure) {
        expenditureMapper.modify(expenditure);

        return Result.success();
    }

    @Transactional
    @Override
    public Result remove(int[] ids) {
        for (int id : ids) {
            expenditureMapper.remove(id);
        }

        return Result.success();
    }

    @Override
    public Result list(Expenditure expenditure) {
        PageHelper.startPage(expenditure.getPageNum(), expenditure.getPageSize());//分页
        String token = request.getHeader("Token");
        Integer storeId = (Integer) JwtUtil.getClaims(token).get("storeId");
        expenditure.setStoreId(storeId);
        List<Expenditure> list = expenditureMapper.list(expenditure);

        // 查询关联公司信息
        List<Integer> collect = list.stream().map(Expenditure::getStoreId).collect(Collectors.toList());
        List<Company> companies = companyApiService.listByIds(collect);
        Map<Integer,Company>companyMap = companies.stream().collect(Collectors.toMap(company -> company.getId(), company -> company));
        list.stream().forEach(bor ->{
            bor.setCompany(companyMap.get(bor.getStoreId()));
        });
        PageInfo<Expenditure> pageInfo = new PageInfo<>(list);
        Map<String, Object> map = new HashMap<>();
        map.put("list", pageInfo.getList());
        map.put("total", pageInfo.getTotal());

        return Result.success(map);
    }

    @Override
    public Result get(int id) {
        Expenditure book = expenditureMapper.get(id);

        return Result.success(book);
    }


    //收入
    @Override
    public Result saveList(Expenditure expenditure) {
        expenditure.setType("1");
        expenditure.setCreateTime(new Date());
        expenditureMapper.save(expenditure);

        return Result.success();
    }
}
